<?php

class Core_Model_DbTable_Configs extends YouNet_Db_Table
{
    protected $_name = 'configs';
    protected $_rowClass = 'Core_Model_Config';
    private $_social;
    static private $_instance;
    
    public function init()
    {
    	parent::init();
    	$this->_social = YouNet_Api::getInstance()->getSocialPlugin();
    }
    
    /*
     * Instance
     */
    public static function getInstance()
    {
        if (!(self::$_instance instanceof Core_Model_DbTable_Configs))
        {
            self::$_instance = new Core_Model_DbTable_Configs();
        }
        return self::$_instance;
    }
    /*
     * Insert page
     */
    public function insertFansConfig($data)
    {
         return $this->insert($data);
    }
    
    /*
     * Get page by id
     */    
    public function hasFansConfig($id_page,$page_type)
    {
            $select = $this->select()                    
                    ->where('id_page = ?', $id_page)
                    ->where('page_type = ?',$page_type);
            $result = $this->fetchAll($select)->current();
            if ($result) 
            {
                return $result['id'];
            }else
            {
                return '';
            }
    }
    public function updateFansConfig( $data,$where)
    {   
        return $this->update($data,$where);
    }
    public function getconfigs($id_page,$page_type){
        
        $select = $this->select()                    
                    ->where('id_page = ?', $id_page)
                    ->where('page_type = ? ',$page_type)
                    ->where('content_type > 0 ');
        $result = $this->fetchAll($select);
        return $result->current();
        
    }
     /**
     * Description : Insert data from array values
     * @param type $arrValidValues
     * @return type
     */
    public function insert_config($arrValidValues, $id_page, $page_type){
        
        $status = 0;
        
         $content_type = $arrValidValues['hdf_type'];
         //$page_type =0;//for fans of not fan 
         
         $data =array(
            "content_type" => $content_type,
            "id_page"      => $id_page,
            "page_type"    => $page_type,
            "created_date" => time()
        );
         
        if ($content_type == 1)//image
        {
            $data["image_url"] = $arrValidValues["image_url"];
            $data["image"] = $arrValidValues["image"];
           
        }elseif ($content_type == 2) 
        {
            $data["html"] = $arrValidValues["html"];
        }elseif ($content_type == 3) 
        {
            $data["iframe_url"] = $arrValidValues["iframe_url"];
            $data["iframe_height"] = $arrValidValues["iframe_height"];
            $data["iframe_hide_scroll"] = $arrValidValues["iframe_hide_scroll"];
        }
        
        //insert 
        $id_config =0; 
        
        if ($this->hasFansConfig($id_page,$page_type))
        {
            unset($data["created_date"]);
            if ($content_type == 1)
            {
                if (!$data["image"]  )
                {
                    unset($data["image"]);
                }
            }
            
            if ($this->updateFansConfig($data,array('id_page = ? '=>$id_page,'page_type = ? ' => $page_type)))
            {
                $status  = 1;
            }
        }
        else
        {
            $id_config= $this->insertFansConfig($data);
            $status  = 1;
        }
        
        return $status ;
    }
    
    public function update_checkshow($val_checkshow,$page_type,$id_page)
    {
        $status  = 0;
        
        $ret = $this-> updateFansConfig(array('content_type'=>$val_checkshow),
                                     array('id_page = ? '=>$id_page,'page_type = ? ' => $page_type ));
        
        if ($ret)
        {
            $status  = 1;
        }
        return $status;
    }
    public function get_newest_updated_date($id_page){
        $select = $this->select()
                    ->from('configs',array('updated_date' =>'MAX(updated_date)'))
                    ->where('id_page = ?', $id_page);
        $result = $this->fetchAll($select)->current();
        
        return $result['updated_date'];
    }
}